package com.cykj.eurekaclient.utils;

import com.cykj.eurekaclient.bean.RouteInfo;
import com.cykj.eurekaclient.bean.RouteTimeNode;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @version 1.0
 * @author: zhiMing
 * @date: 2021/9/23 1:00
 * @desc: 将从数据库获取的时刻进行按时段进行分组，这个一个分组的工具类
 */
public class RouteInfoTool {

    private String timeParagraph;
    private String timeParagraphState;
    private List<RouteTimeNode> timeNodeList;

    public static List<RouteInfoTool> getRouteTimeNodeList(RouteInfo routeInfo){
        List<RouteInfoTool> routeInfoToolList = new ArrayList<>();
        try {
           List<RouteTimeNode> list =  routeInfo.getRouteTimeNodeList();
//          Collections.sort(list,Collections.reverseOrder());
//            Collections.reverse(list);// 将数据进行倒序
           /*获取集合的迭代器，没操作一个数据就从旧数组中去除*/
           Iterator<RouteTimeNode> iterator = list.listIterator();
           while (iterator.hasNext()){
               /*获取时间节点对象信息*/
               RouteTimeNode routeTimeNode = iterator.next();
               /*获取该时间节点对应的时段*/
               String timeParagraph = routeTimeNode.getTimeParagraphStr();
               String timeParagraphState = String.valueOf(routeTimeNode.getState());
//               System.out.println(timeParagraphStr+" "+routeTimeNode.getTimeNode());
               boolean bool = false;
               for (RouteInfoTool tool: routeInfoToolList) {
                   /*当时段已存在routeInfoToolList集合中时，则直接添加*/
                   if (tool.timeParagraph.equals(timeParagraph)){
                       tool.timeNodeList.add(routeTimeNode);
                       iterator.remove();
                       bool = true;
                   }
               }
               /*当时段不存在routeInfoToolList集合中时，
                  则重新实例一个存放路线时刻的集合，并重新实例工具类，
                  然后将工具类存放进routeInfoToolList集合中*/
               if (!bool){
                   List<RouteTimeNode> timeNodeList = new ArrayList<>();
                   timeNodeList.add(routeTimeNode);
                   RouteInfoTool routeInfoTool = new RouteInfoTool(timeParagraph,timeParagraphState,timeNodeList);
                   routeInfoToolList.add(routeInfoTool);
                   iterator.remove();
               }
           }

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
        if (routeInfoToolList.size()==0)
            return null;
        else {
            /*将就集合制空*/
            routeInfo.setRouteTimeNodeList(null);
            return routeInfoToolList;
        }
    }

    public RouteInfoTool() {
    }

    public RouteInfoTool(String timeParagraph,String timeParagraphState, List<RouteTimeNode> timeNodeList) {
        this.timeParagraph = timeParagraph;
        this.timeParagraphState = timeParagraphState;
        this.timeNodeList = timeNodeList;
    }

    public String getTimeParagraphState() {
        return timeParagraphState;
    }

    public void setTimeParagraphState(String timeParagraphState) {
        this.timeParagraphState = timeParagraphState;
    }

    public String getTimeParagraph() {
        return timeParagraph;
    }

    public void setTimeParagraph(String timeParagraph) {
        this.timeParagraph = timeParagraph;
    }

    public List<RouteTimeNode> getTimeNodeList() {
        return timeNodeList;
    }

    public void setTimeNodeList(List<RouteTimeNode> timeNodeList) {
        this.timeNodeList = timeNodeList;
    }
}
